W9. Randomized Algorithms
1. Theory
1.1 Probability Basics
1.1.1 Sample Spaces and Events
To reason precisely about randomness, we need a formal vocabulary. The starting point is the sample space
An event is any subset of the sample space. For instance, the event “exactly one tail” corresponds to:
This formal setup lets us reason carefully about what can happen and how likely each outcome is.
1.1.2 Axioms of Probability
A probability distribution is a function
for any event — probabilities are non-negative. — the probability of some outcome happening is 1. for any two mutually exclusive events and (i.e., ). for any finite or countably infinite sequence of pairwise mutually exclusive events.
1.1.3 Basic Properties of Probability
The following properties follow directly from the axioms:
Proof:
, so . Since , we get , hence . where is the complement of .Proof:
and , so .Proof: We can split
into the disjoint parts and . Then , so .
Example (coin tossing): Tossing a fair coin 3 times. Assuming all 8 outcomes are equally likely, each has probability
1.2 Discrete Probability Distributions
A probability distribution is called discrete if it is defined on a finite or countably infinite sample space.
A special case is the uniform distribution: if
Examples:
- Rolling a fair six-sided die:
with each outcome having probability . This is uniform. - Sum of two fair dice: The sum ranges from 2 to 12, but not all sums are equally likely (e.g., a sum of 7 is more likely than 2). This is not uniform.
- Choosing a random student ID from a class of
students: uniform (each ID equally likely).
1.3 Discrete Random Variables
A discrete random variable
We define:
The function
1.3.1 Expected Value
The expected value (also called the expectation or mean) of a random variable
The expected value tells you what the “average” outcome would be if you repeated the experiment many times. For example, if you toss a fair coin and gain 1 point for heads and 0 for tails, the expected value is
Example: In a game where tossing two coins gives
1.3.2 Linearity of Expected Value
Expected value is a linear operator, meaning it satisfies:
This holds for any random variables
If
Note that in general
When does
1.3.3 Independent Random Variables
Random variables
for all values
Example of dependent variables: Let
Multiplication rule for independent variables: If
This is stronger than the additivity property and only holds when
1.4 Indicator Random Variables
An indicator random variable (also called a Bernoulli random variable) for event
Indicator variables are the main tool for converting probability statements into expected value calculations. The key lemma connects them:
Lemma (Cormen et al. 2022, Lemma 5.1). Let
Proof:
Why are indicator variables useful? They let us decompose complex random variables into sums of simple ones. If
This is much easier to compute than reasoning directly about the distribution of
Example (expected number of heads): When tossing a fair coin
1.5 Probabilistic Analysis
Probabilistic analysis is the study of an algorithm’s behavior (especially its running time) using probability theory. Instead of asking “what is the worst possible running time?”, we ask “what is the expected running time averaged over all possible inputs?”
To apply probabilistic analysis, we need:
- A known or assumed distribution of inputs.
- Justified assumptions that the distribution is realistic for the use case.
- Computation of the expected running time by averaging over all inputs under that distribution.
If we cannot justify a reasonable input distribution, probabilistic analysis may not be applicable.
1.5.1 The Hiring Problem
Imagine you are hiring an AI agent for a task. There are
HIRE-ASSISTANT(n)
1 best = 0 // candidate 0 is a least-qualified dummy
2 for i = 1 to n
3 interview candidate i // cost c_c
4 if candidate i is better than candidate best
5 best = i
6 hire candidate i // cost c_h
Worst case: Candidates arrive in increasing order of quality — we hire every candidate. Total hiring cost:
Best case: The best candidate arrives first — we hire only once. Total hiring cost:
Probabilistic analysis: Assume candidates arrive in a uniformly random order (each of the
The expected number of hires is:
This uses the fact that
1.6 Randomized Algorithms
The probabilistic analysis above required assuming a uniform random input. But what if candidates arrive in a specific (possibly adversarial) order? A randomized algorithm takes control of this uncertainty: instead of hoping the input is random, it makes the input random.
Definition: An algorithm is randomized if its behavior depends not only on the input but also on random choices made during execution (values from a random-number generator).
Randomized hiring: Before running HIRE-ASSISTANT, randomly shuffle the candidates using:
RANDOMLY-PERMUTE(A, n)
1 for i = 1 to n
2 swap A[i] with A[RANDOM(i, n)]
This produces a uniformly random permutation of the candidates. Now, regardless of the original input order, we can guarantee that the expected number of hires is
The key insight: probabilistic analysis relies on the input being random; randomized algorithms rely on the algorithm itself making random choices.
1.7 Randomized Quicksort
Quicksort is a classic divide-and-conquer sorting algorithm with the following properties:
- It is a divide-and-conquer algorithm.
- It runs in
on average but in the worst case. - It is stable (equal elements maintain their relative order).
- It has an efficient in-place implementation (no extra array needed).
The idea of the algorithm:
- Divide: Partition the array around a pivot element — put all elements smaller than the pivot before it, all larger elements after it.
- Conquer: A single-element subarray is already sorted.
- Combine: Concatenate the sorted subarrays with the pivot in the middle.
The worst case occurs when, e.g., the array is already sorted and we always pick the first or last element as pivot.
Randomized pivot selection avoids the worst case by choosing the pivot uniformly at random:
RANDOMIZED-PARTITION(A, p, r)
1 i = RANDOM(p, r)
2 exchange A[r] with A[i]
3 return PARTITION(A, p, r)
RANDOMIZED-QUICKSORT(A, p, r)
1 if p < r
2 q = RANDOMIZED-PARTITION(A, p, r)
3 RANDOMIZED-QUICKSORT(A, p, q - 1)
4 RANDOMIZED-QUICKSORT(A, q + 1, r)
1.7.1 Analysis of Randomized Quicksort
Lemma (Cormen et al. 2022, Lemma 7.1). If
To estimate the expected number of comparisons, let
Then
Key observation: Two elements
- If a pivot
with is chosen before either or , they are separated into different subproblems and will never be compared. - If
or is chosen as pivot first (before any with ), they are compared directly.
Since each of the
Therefore, the expected total number of comparisons is:
Summary of randomized quicksort:
- Works just like ordinary quicksort, but chooses the pivot randomly.
- Expected running time:
on any input. - Worst-case running time: still
, but now it depends on the random number generator, not the input — an adversary cannot force the worst case by choosing a bad input.
1.8 Probabilistic Analysis of Bucket Sort
Bucket sort is a linear-time sorting algorithm that works under a specific assumption: the input values are uniformly distributed over some interval (typically
Algorithm:
- Divide
into equal-sized subintervals (“buckets”). Each subinterval has the same probability of receiving an element, since the input is uniform. - Assign each input element to the bucket corresponding to its subinterval: element
goes to bucket (a cheap rounding operation). - Sort each bucket using insertion sort.
- Concatenate the sorted buckets.
Why does this work fast? If the input is uniformly distributed, each bucket expects to receive
1.8.1 Formal Analysis
Let
(The
We want to compute
Let
Computing
First sum: Since
Second sum: For
There are
Therefore, the expected total running time is:
Bucket sort runs in
1.9 Randomly Built Binary Search Trees
A binary search tree (BST) is a binary tree satisfying the BST property: for every node
The shape (and thus efficiency) of a BST depends entirely on the order in which keys are inserted. When keys are inserted in a uniformly random order, the resulting tree is called a randomly built BST.
1.9.1 Two Different Notions of “Random BST”
It is important to distinguish two concepts:
- Randomly chosen BST: Pick uniformly at random among all valid BST shapes on
nodes. The number of such shapes is the -th Catalan number . - Randomly built BST: Start with an empty tree and insert a uniformly random permutation of
distinct keys one by one. Each of the permutations is equally likely, but different permutations can produce the same tree shape, so this does not give a uniform distribution over tree shapes.
These two notions differ: for
Expected height of a randomly built BST: It is known (Cormen et al. 2022, §12.4) that the expected height of a randomly built BST on
2. Definitions
- Sample space (
): The set of all possible outcomes of a random experiment. - Event: Any subset of the sample space
. - Probability distribution: A function
satisfying the four axioms of probability; assigns a real number to every event. - Discrete probability distribution: A probability distribution defined on a finite or countably infinite sample space.
- Uniform distribution: A discrete distribution on a finite sample space where every outcome has equal probability
. - Discrete random variable: A function
mapping outcomes to real numbers on a finite or countably infinite sample space. - Probability mass function: The function
describing the probability distribution of a discrete random variable . - Expected value (
): The probability-weighted average of a random variable: . - Linearity of expectation: The property
, which holds for any random variables regardless of independence. - Independent random variables: Random variables
and such that for all . - Indicator random variable (
): A random variable that equals 1 if event occurs and 0 otherwise; its expected value equals . - Probabilistic analysis: The analysis of an algorithm’s performance using probability theory, typically computing the expected running time over a distribution of inputs.
- Expected running time: The average running time of an algorithm computed by averaging over all possible inputs (or random choices) weighted by their probabilities.
- Randomized algorithm: An algorithm whose behavior depends on the input and on values produced by a random-number generator; it takes internal random choices rather than relying on the input being random.
- Random permutation: A permutation of a set chosen uniformly at random from all
permutations; generated in place by the RANDOMLY-PERMUTE algorithm. - Binary search tree (BST): A binary tree satisfying the BST property: keys in the left subtree of any node are
that node’s key, and keys in the right subtree are that node’s key. - Randomly built BST: A BST obtained by inserting
distinct keys one by one in a uniformly random order; has expected height . - Catalan number: The number of distinct binary tree shapes on
nodes, given by . - Pivot (quicksort): The element chosen to partition the array during quicksort; in randomized quicksort, chosen uniformly at random from the current subarray.
- Bucket sort: A sorting algorithm that distributes elements into
equal-width buckets, sorts each bucket with insertion sort, and concatenates results; runs in expected time when input is uniformly distributed over .
3. Formulas
- Probability of complementary event:
- Inclusion-exclusion (two events):
- Expected value:
- Linearity of expectation:
and - Function of random variable:
- Independence (product rule): If
and are independent, then - Indicator variable lemma:
- Expected hires (hiring problem):
- Probability of comparison (quicksort):
- Expected comparisons (quicksort):
- Expected bucket size squared:
- Catalan number:
- Harmonic number:
4. Practice
4.1. Prove or Disprove Probability Identities (Problem Set 7, Task 1)
For each of the following equalities, either prove it using the axioms of probability and basic set identities, or provide a counterexample:
(a)
(b)
Click to see the solution
(a)
This is false in general. We provide a counterexample.
Let
Then:
Since
The correct formula is:
(b)
This is false in general. We provide a counterexample.
Let
, so , thus . , so , thus .
This happens to be equal here. Let’s try
, so : . , so : .
Equal again. Let’s try
, so : . , so : .
Since
Answer: (a) False — counterexample given above. (b) False — counterexample with
4.2. Prove or Disprove Expectation Identities (Problem Set 7, Task 2)
For each of the following, either prove it or provide a counterexample. Assume all random variables take real values and all expected values exist.
(a)
(b)
(c)
(d)
Click to see the solution
(a)
This is true. By linearity of expectation:
(b)
This is false in general, even with independence.
Counterexample: Let
Then:
(by independence)
Since
The correct statement would use Jensen’s inequality: since
(c)
This is false in general.
Counterexample: Let
, so . only when (probability ); otherwise . .
Since
(d)
This is false in general.
Counterexample: Let
, so . when and when , so .
This happens to be equal here, but the equality is coincidental. Let
, so . .
The equality
Answer: (a) True by linearity. (b) False — counterexample given. (c) False — counterexample given. (d) False — counterexample given.
4.3. Modular-Search Algorithm (Problem Set 7, Task 3)
Consider a Modular-Search algorithm that traverses an array using modular arithmetic: fix a constant step
The array
(a) Give a high-level description and pseudocode for Modular-Search.
(b) Analyze worst-case, best-case, and expected running time.
(c) Write pseudocode for Randomized-Search (using a random permutation instead of a fixed step).
(d) Derive the expected running time of Randomized-Search.
(e) Compare Modular-Search and Randomized-Search: when is the deterministic algorithm slow, and how does randomization help?
Click to see the solution
Key Concept: Modular-Search is a deterministic traversal; Randomized-Search uses a random permutation to avoid adversarial worst cases.
(a) Pseudocode for Modular-Search:
MODULAR-SEARCH(A, n, x, s)
1 idx = 1
2 for step = 1 to n
3 if A[idx] == x
4 return idx // found x
5 idx = (idx - 1 + s) mod n + 1 // advance by s modulo n (1-indexed)
6 return NOT_FOUND
High-level description: We probe the array at positions
(b) Running time analysis:
Worst case: An adversary places all
For
Best case: The very first position visited contains a copy of
Expected case (assuming the
Since all
(c) Pseudocode for Randomized-Search:
RANDOMIZED-SEARCH(A, n, x)
1 pi = RANDOMLY-PERMUTE([1..n]) // generate a random permutation of indices
2 for i = 1 to n
3 if A[pi[i]] == x
4 return pi[i] // found x
5 return NOT_FOUND
(d) Expected running time of Randomized-Search:
For any fixed array with exactly
Using indicator variables: let
By a cleaner argument: the first occurrence of
This holds for any fixed input, since randomness comes from the permutation, not the input.
(e) Comparison:
When is Modular-Search slow? An adversary who knows the step
How does randomization help? Randomized-Search generates a new random permutation for each run. Even if an adversary knows the algorithm, they cannot predict the permutation, so they cannot arrange the data to force the worst case. For any fixed input with
Answer: Modular-Search has worst-case
4.4. Determining Uniform Distributions (Lecture 7, Example 1)
For each of the following, determine whether the probability distribution is uniform:
(a) Rolling a fair six-sided die.
(b) Rolling two fair six-sided dice and looking at the sum of the faces.
(c) Choosing a random student ID from a class of
Click to see the solution
(a) Rolling a fair six-sided die:
The sample space is
Uniform? Yes.
(b) Sum of two fair dice:
The sample space for individual rolls is
- Sum = 2 occurs only 1 way:
— probability - Sum = 7 occurs 6 ways:
— probability
Uniform? No.
(c) Choosing a random student ID:
By default, “choosing at random” means uniformly at random. Each of the
Uniform? Yes (by convention).
4.5. Dependent Random Variables (Lecture 7, Example 2)
Give an example of two random variables that are not independent.
Click to see the solution
Key Concept: Two variables are dependent if knowing the value of one changes the probability distribution of the other.
Let
Then:
and
But
Since the joint probability does not equal the product of the individual probabilities,
Answer:
4.6. Expected Number of Heads (Lecture 7, Example 3)
When tossing a fair coin
Click to see the solution
Key Concept: Decompose the count into indicator variables and use linearity of expectation.
- Define indicator variables: For each toss
, let: - Express total heads: The total number of heads is
. - Apply linearity of expectation and the indicator lemma:
Answer: The expected number of heads in
4.7. Probabilistic Analysis of the Hiring Problem (Lecture 7, Example 4)
When evaluating
Click to see the solution
Key Concept: Use indicator variables. The
Define indicator variables: For each
, let:Compute probabilities: The
-th candidate is hired iff they are the best of . Since the order is uniformly random, each of the first candidates is equally likely to be the best:Expected total hires:
Concretely: the 1st candidate is always hired (
), the 2nd with probability , the 3rd with probability , and so on.
Answer: The expected number of hires is
4.8. Expected Score in a Coin Game (Lecture 7, Task 1)
Consider a game in which we toss a pair of coins. For each heads you gain 3 points, and for each tails you lose 2 points. What is the expected value of your score?
Click to see the solution
Key Concept: Define a random variable over the sample space, compute
- Define the sample space:
Assuming a fair coin, each outcome has probability . - Define the score random variable
: (two heads) (one head, one tail) (one head, one tail) (two tails)
- Compute the expected value:
Answer: The expected score is
4.9. Expected Height of a Randomly Chosen BST (Lecture 7, Task 2)
What is the expected height of a randomly chosen binary search tree?
(a) With 5 nodes.
(b) With
Click to see the solution
Key Concept: A randomly chosen BST means we pick uniformly at random among all valid BST shapes (not among all insertion orders). The number of distinct BST shapes on
(a) For
Step 1 — Count the shapes. The number of BST shapes on 5 nodes is:
The lecture scratchpad verifies this by grouping shapes by root value. For a BST on keys
| Root |
Left shapes | Right shapes | Subtotal |
|---|---|---|---|
| 1 | 14 | ||
| 2 | 5 | ||
| 3 | 4 | ||
| 4 | 5 | ||
| 5 | 14 | ||
| Total | 42 |
Step 2 — Heights. For
(b) For general
A uniformly random BST shape (one of
This is significantly worse than the
Answer: For
4.10. Expected Height of a Randomly Built BST (Lecture 7, Task 3)
What is the expected height of a randomly built binary search tree obtained by inserting keys one by one into an initially empty tree?
(a) 5 keys.
(b)
Click to see the solution
Key Concept: A randomly built BST inserts keys in a uniformly random permutation order — each of the
Lecture worked example (
because 16 permutations yield trees of height 2 and 8 permutations yield trees of height 3.
(a) For
There are
- The first key inserted becomes the root. If it is 1 or 5, the tree immediately degenerates to a chain on one side.
- Middle root values (2, 3, or 4) create a more balanced split.
The expected height for
(b) For general
It is proven (Cormen et al. 2022, §12.4) that the expected height of a randomly built BST on
The intuition: in a random permutation, the root is equally likely to be any of the
Answer: For